#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import os, sys

from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util, template

from tasks.helpers import get_id, handler, template_path
from tasks.models import (
    Task,
    TaskSwitch,
    get_context,
    switch_task,
    get_tasks_tree,
    create_task,
    update_task,
)

def handle_main(self, context):
    self.response.out.write(template.render(
        template_path('tasks/main.html'),
        dict(
            context = context,
            tasks = get_tasks_tree(context),
            switches = list(TaskSwitch.gql(
                "WHERE "
                "  ANCESTOR IS :1 "
                "ORDER BY started DESC "
                "LIMIT 50",
                context.root,
            )),
        )
    ))

def handle_new_task(self, context):
    parent_task = get_id(self.request, 'parent_task')
    create_task(context, self.request.get('title'), parent_task)
    self.redirect('/tasks/')

def handle_update_task(self, context):
    task = Task.get_by_id(get_id(self.request, 'task'), parent=context.root)
    update_task(context, task,
        title = self.request.get('title', None),
        parent_task = get_id(self.request, 'parent_task'),
    )
    self.redirect('/tasks/')

def handle_switch_task(self, context):
    task = Task.get_by_id(get_id(self.request, 'task'), parent=context.root)
    switch_task(context, task)
    self.redirect('/tasks/')

def handle_delete_task(self, context):
    task = Task.get_by_id(get_id(self.request, 'task'), parent=context.root)
    task.deleted = True
    task.put()
    self.redirect('/tasks/')

def handle_update_task_switch(self, context):
    switch = TaskSwitch.get_by_id(get_id(self.request, 'task_switch'), parent=context.root)
    switch.comment = self.request.get('comment')
    switch.put()
    self.redirect('/tasks/')

def main():
    application = webapp.WSGIApplication([
            ('/tasks/',        handler(get=handle_main)),
            ('/tasks/new',     handler(post=handle_new_task)),
            ('/tasks/update',  handler(post=handle_update_task)),
            ('/tasks/switch',  handler(post=handle_switch_task)),
            ('/tasks/delete',  handler(post=handle_delete_task)),
            ('/tasks/update_task_switch', handler(post=handle_update_task_switch)),
        ],
        debug = True,
    )
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()
